package com.tisumoon;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.tisumoon.lib.R;

public class Notifications {
	public static final String TAG = "com.tisumoon";

	public static final int TYPE_YO			= 0;
	public static final int TYPE_MAIL			= 1;
	private static int mYoId;

	@SuppressWarnings("deprecation")
	public static void showNotification(Context context, int type, String title, String content) {

		Notification notice = new Notification();
		notice.tickerText = null;	//这里如果为null，则状态栏直接显示一个图标；否则会滚动显示这里的文字

		//放到正在运行的组,正在运行的任务不会清除
		//notice.flags |= Notification.FLAG_ONGOING_EVENT;
		//展开通知下拉列表，点击清除按钮的时候不会清除这个通知(有了上面的，可不要这行)
		//notice.flags |= Notification.FLAG_NO_CLEAR;
		//当该通知被点击之后，此通知自动清除
		notice.flags |= Notification.FLAG_AUTO_CANCEL;
		notice.flags |= Notification.FLAG_SHOW_LIGHTS;
		//notice.defaults = Notification.DEFAULT_LIGHTS;
		//notice.ledARGB = Color.BLUE;
		//notice.ledOnMS = 500;
		//notice.ledOffMS = 500;
		//notice.number = 2;
		//notice.contentView=new MyRemoteViews(getPackageName(), R.layout.xxx);

		//notice.defaults |= Notification.DEFAULT_VIBRATE;
		// 100 毫秒延迟后，震动 250 毫秒，暂停 100 毫秒后，再震动 500 毫秒 
		//notice.vibrate = new long[] {100, 250, 100, 500};
		Intent intent;
		PendingIntent contentIntent;

		switch (type) {
		case TYPE_YO:
			mCurrentType = TYPE_YO;
			notice.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
			notice.ledARGB = context.getResources().getColor(R.color.bg_color_title_yellow);
			notice.ledOnMS = 800;
			notice.ledOffMS = 300;
			notice.icon = R.drawable.exp_i_f_chitchat_friend_goodmorning;

			intent = new Intent(Const.Broadcast.YO_CLICK_NOTIFY);
			/*intent.setAction(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_LAUNCHER);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);*/

			contentIntent = PendingIntent.getBroadcast(context, TYPE_YO, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			/*Builder builder = new Notification.Builder(context)
	         .setContentTitle(title)
	         .setContentText(content)
	         .setDefaults(Notification.DEFAULT_ALL)
			.setLights(context.getResources().getColor(R.color.bg_color_title_yellow), 300, 300);
	         .setSmallIcon(R.drawable.exp_i_f_chitchat_friend_goodmorning);*/

			notice.tickerText = title + ":" + content;
			notice.setLatestEventInfo(context, title, content, contentIntent);
			//太多不清理的话，后面的会不显示
			getNotificationManager(context).cancel(TAG, mYoId - 5);
			getNotificationManager(context).notify(TAG, mYoId++, notice);
			break;
		case TYPE_MAIL:
			mCurrentType = TYPE_MAIL;
			notice.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
			notice.ledARGB = context.getResources().getColor(R.color.bg_color_title_yellow);
			notice.ledOnMS = 800;
			notice.ledOffMS = 300;
			notice.icon = R.drawable.exp_i_f_chitchat_friend_goodmorning;

			intent = new Intent(Const.Broadcast.YO_MAIL_CLICK_NOTIFY);

			contentIntent = PendingIntent.getBroadcast(context, TYPE_MAIL, intent, PendingIntent.FLAG_UPDATE_CURRENT);

			notice.tickerText = title;
			notice.setLatestEventInfo(context, title, content, contentIntent);
			//太多不清理的话，后面的会不显示
			getNotificationManager(context).cancel(TAG, mYoId - 5);
			getNotificationManager(context).notify(TAG, mYoId++, notice);
			break;
			
			
			/*case TYPE_RESTART:
			mCurrentType = TYPE_RESTART;

			notice.defaults = Notification.DEFAULT_LIGHTS;

			int icon = R.drawable.logo;
			DownloadClient client = DownloadClient.getInstance();
			if(client != null) {	//如果正在进行下载
				if(client.isLoading()) {
					icon = R.drawable.anim_download_bg;
				}
			}
			notice.icon = icon;
			intent = new Intent(context, StartActivity.class);

			 * 以下两行是关键，必须要，同时要让该intent的目标Activity（即getClass()所在的Activity）的
			 * 启动模式为标准模式(默认就是标准模式android:launchMode="standard")，才能不启动目标Activity，
			 * 而直接恢复到task的栈顶。如果想要在点击通知后启动目标Activity的除外。

			intent.setAction(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_LAUNCHER);
			//这行可不要，但有时候还是非常有用的，具体请看相关标记(FLAG)的说明
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

			contentIntent = PendingIntent.getActivity(
					context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			notice.setLatestEventInfo(context, context.getResources().getString(R.string.app_name),
					context.getResources().getString(R.string.notify_message_click_restart), contentIntent);
			break;
		case TYPE_FG_DOWNLOAD:
			mCurrentType = TYPE_FG_DOWNLOAD;

			notice.ledARGB = Color.BLUE;
			notice.ledOnMS = 500;
			notice.ledOffMS = 500;

			notice.icon = mShowProgress ? R.drawable.logo : R.drawable.anim_download_bg;
			intent = new Intent(context, StartActivity.class);
			intent.setAction(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_LAUNCHER);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

			contentIntent = PendingIntent.getActivity(
					context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			notice.setLatestEventInfo(context, context.getResources().getString(R.string.app_name),
					context.getResources().getString(R.string.notify_message_isloading), contentIntent);
			break;
		case TYPE_BG_DOWNLOAD:
			mCurrentType = TYPE_BG_DOWNLOAD;

			notice.ledARGB = Color.BLUE;
			notice.ledOnMS = 500;
			notice.ledOffMS = 500;

			notice.icon = mShowProgress ? R.drawable.logo : R.drawable.anim_download_bg;
			intent = new Intent(context, DownloadService.class);
			intent.putExtra(ExtraKeys.STOP, true);
			contentIntent = PendingIntent.getService(
					context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			notice.setLatestEventInfo(context, context.getResources().getString(R.string.app_name),
					context.getResources().getString(R.string.notify_message_click_stop_bg_download), contentIntent);
			break;*/
		}
	}

	/*public static void showNotificationProgress(Context context, int notifId, CharSequence describe, CharSequence totleSize, int max, int progress) {
		Notification notice = new Notification();

		notice.icon = R.drawable.anim_download_bg;

		notice.flags |= Notification.FLAG_AUTO_CANCEL;
		notice.flags |= Notification.FLAG_SHOW_LIGHTS;
		notice.ledARGB = Color.GREEN;
		notice.ledOnMS = 250;
		notice.ledOffMS = 250;

		notice.contentView = getRemoteViews(context.getPackageName());
		notice.contentView.setTextViewText(R.id.notification_text, describe);
		notice.contentView.setTextViewText(R.id.notification_percent,
				NumberUtils.floatFormat(progress*100f/max, 3, 2, 0)+ "%" + (totleSize==null ? "" : "("+totleSize+")"));
		notice.contentView.setProgressBar(R.id.notification_progressbar, max, progress, false);

		notice.contentIntent = PendingIntent.getService(
				context, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);

		getNotificationManager(context).notify(TAG, notifId, notice);

		mShowProgress = true;
		showNotification(context, mCurrentType);
	}

	private static RemoteViews mRemoteViews;
	private static RemoteViews getRemoteViews(String pkgName) {
		if(mRemoteViews == null) mRemoteViews = new RemoteViews(pkgName, R.layout.notification_progress);
		return mRemoteViews;
	}

	public static void removeNotificationProgress(Context context, int notifId) {
		getNotificationManager(context).cancel(TAG, notifId);
		mShowProgress = false;
		showNotification(context, mCurrentType);
	}*/

	public static void removeNotification(Context context, int type) {
		if(type == mCurrentType) {
			//没有tag，竟然清除不掉
			getNotificationManager(context).cancel(TAG, 0);
			mCurrentType = -1;
		}
	}

	public static void removeAllNotification(Context context) {
		getNotificationManager(context).cancel(TAG, 0);
		mCurrentType = -1;
	}

	private static NotificationManager getNotificationManager(Context context) {
		return (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
	}

	//private static boolean mShowProgress = false;
	private static int mCurrentType = -1;
}
